<?php


/*
 * Created on 10/01/2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function echoStat($stat, $positionStat) {
    if ($stat == $positionStat) {
        echo "  <td>" . getEchoValue($stat) . "</td>\n";
    } else {
        echo "  <td><b>" . getEchoValue($stat) . "</b></td>\n";
    }
}

function echoPlayerRow($player, $position, $usesExps) {
    echo "<tr align=center>\n";
    echo "  <td>$player->rosterNum</td>\n";
    echo "  <td align=left class=\"small\">" . getEchoValue($player->name) . "</td>\n";
    echo "  <td align=left class=\"small\">" . getEchoValue($player->position) . "</td>\n";
    if (!$position) {
        echo "  <td>" . getEchoValue($player->ma) . "</td>\n";
        echo "  <td>" . getEchoValue($player->st) . "</td>\n";
        echo "  <td>" . getEchoValue($player->ag) . "</td>\n";
        echo "  <td>" . getEchoValue($player->av) . "</td>\n";
    } else {
        echoStat($player->ma, $position->ma);
        echoStat($player->st, $position->st);
        echoStat($player->ag, $position->ag);
        echoStat($player->av, $position->av);
    }
    echo "  <td align=left class=\"small\">" . getEchoValue($player->skills) . "</td>\n";
    echo "  <td>" . getEchoValue($player->getInjuries()) . "</td>\n";
    echo "  <td>" . getEchoValue($player->completions) . "</td>\n";
    echo "  <td>" . getEchoValue($player->tds) . "</td>\n";
    echo "  <td>" . getEchoValue($player->interceptions) . "</td>\n";
    echo "  <td>" . getEchoValue($player->casualties) . "</td>\n";
    if ($usesExps) {
        echo "  <td>" . getEchoValue($player->exps) . "</td>\n";
    } else {
        echo "  <td>" . getEchoValue($player->mvps) . "</td>\n";
    }
    echo "  <td>" . getEchoValue($player->getSPPS()) . "</td>\n";
    echo "  <td align=\"right\">" . getEchoValue($player->cost) . "k</td>\n";
    echo "</tr>\n";
}

function echoPMR($pmr, $matchDao) {
    if ($pmr->matchRecord->matchId) {
        $match = $matchDao->findMatch($pmr->matchRecord->matchId);
        global $teams;

        if ($match->homeRec->id == $pmr->matchRecord->id) {
            $opposingTeam = $teams->getTeam($match->awayRec->teamId);
        } else {
            $opposingTeam = $teams->getTeam($match->homeRec->teamId);
        }
        // http://slobb.doubleskulls.net/match_view.php?matchId=29
        $link = $opposingTeam->getRosterLink();
        echo<<< EOQ
            <tr>
            	<td><a href="match_view.php?matchId=$match->id" title="Match Details">more</a></td>
            	<td>vs&nbsp;$link</td>
EOQ;
    } else {
        echo<<< EOQ
            <tr>
            	<td colspan="2"><i>Unidentified match</i></td>
EOQ;
    }
    echo<<< EOQ
            	<td>$pmr->comps</td>
            	<td>$pmr->tds</td>
            	<td>$pmr->ints</td>
            	<td>$pmr->cas</td>
EOQ;
    if ($team->usesExp) {
        echo<<< EOQ
            <td>$pmr->exps</th>
EOQ;
    } else {
        echo<<< EOQ
            <td>$pmr->mvps</th>
EOQ;
    }
    echo "<td>" . $pmr->getSPPs() . "</td>";
    $skill = "&nbsp;";
    if (isset ($pmr->skill)) {
        $skill = $pmr->skill->name;
    }
    echo<<< EOQ
            	<td>$skill</td>
            	<td>$pmr->injury</td>
EOQ;

}

include_once ("includes/init.php");
$playerId = getParam("playerId");

if (!isset ($playerId)) {
    $redirectStr = "No such player";
    $message = "Unable to identify player to show";
    include ("includes/redirect.php");
    return;
}

$playerDao = new PlayerDao();
$player = $playerDao->loadPlayer($playerId);

if (!$player) {
    $redirectStr = "No such player";
    $message = "Unable to identify player to show";
    include ("includes/redirect.php");
    return;
}

$team = $teams->getTeam($player->teamId);
if (!$team) {
    $redirectStr = "No such team";
    $message = "Unable to identify team of player to show";
    include ("includes/redirect.php");
    return;
}

$submit = getParam("submit");
$canEdit = isset($loggedin)  && $loggedin->id == $team->coach->id; 
if ($canEdit && isset($submit)) {
    $rosterNum = getParam("roster_num");
    $player->name = getParam("name");
    if ($rosterNum != $player->rosterNum) {
    	$team->changeRosterNumber($player, $rosterNum);
    }
    $team->setPlayer($player);
    $team->save();
    $teams->setTeam($team);
    $_SESSION["teams"]=$teams;
}
$submitComment = getParam("submitComment");
if ($canEdit && isset($submitComment)) {
	$comments = getParam("comments");
    $player->comments = stripslashes($comments);
    $team->setPlayer($player);
    $team->save();
    $teams->setTeam($team);
    $_SESSION["teams"]=$teams;
}

$title = "$team->name #$player->rosterNum $player->name $player->position";
include_once ("includes/header.php");
echo "<caption><h1>" . $team->getRosterLink();
echo<<< EOQ
	<br />#$player->rosterNum $player->name $player->position</h1></caption>
	<tr><td>
EOQ;
	if ($canEdit) {
echo<<< EOQ
<div id="EditComment" style="display: none">
  <table width="100%" cellspacing="5">
    <tr>
	  <form method="post">
      <td class="small" align="center">
      <input type="submit" name="submitComment" value="Update">
      <br /><br /><a href="javascript:comment()">[Cancel]</a> 
      </td>
      <td align="left"><TEXTAREA name="comments" rows="5" cols="100">$player->comments</TEXTAREA></td>
      </form>
    </tr>
  </table>
</div>

<script language=javascript type='text/javascript'>
function edit() {
	changeDivDisplay('EditComment', 'block');
	changeDivDisplay('ShowComment', 'none');
}

function comment() {
	changeDivDisplay('EditComment', 'none');
	changeDivDisplay('ShowComment', 'block');
}
</script> 
EOQ;
	}
echo<<< EOQ
	<div id="ShowComment" style="display: block">
EOQ;
	
    if ($canEdit) {
        echo<<< EOQ
		<a href="javascript:edit()" class="sup">[Edit]</a>&nbsp;
EOQ;
    }
    echo<<< EOQ
    <pre class="wrap">$player->comments</pre></div></td></tr>
	<tr><td><table border="0" cellspacing="0" cellpadding="4" width="100%">
	<tr>
            <th><span title="Roster Number">#</span></th>
            <th>Player Name</th>
            <th>Position</th>
            <th><span title="Movement">ma</span></th>
            <th><span title="Strength">st</span></th>
            <th><span title="Agility">ag</span></th>
            <th><span title="Armour">av</span></th>
            <th><span title="Skills and other">Skills</span></th>
            <th><span title="Injuries - MNG and Niggles">inj</span></th>
            <th><span title="Completions">cp</span></th>
            <th><span title="Touchdowns">td</span></th>
            <th><span title="Interceptions">in</span></th>
            <th><span title="Casualties">cs</span></th>
EOQ;
if ($team->usesExp) {
    echo<<< EOQ
            <th><span title="Experience">exp</span></th>
EOQ;
} else {
    echo<<< EOQ
            <th><span title="Most Valuable Players">mvp</span></th>
EOQ;
}
echo<<< EOQ
            <th>spps</th>
            <th>Cost</th>
	</tr>
EOQ;
echoPlayerRow($player, $team->race->getPosition($player->position), $team->usesExp);
echo<<< EOQ
	</table></td></tr>
EOQ;

echo<<< EOQ
	<tr><td><table border="0" cellspacing="0" cellpadding="4" width="100%">
	<tr>
            <th>&nbsp;</th>
            <th>Opposing Team</th>
            <th><span title="Completions">cp</span></th>
            <th><span title="Touchdowns">td</span></th>
            <th><span title="Interceptions">in</span></th>
            <th><span title="Casualties">cs</span></th>
EOQ;
if ($team->usesExp) {
    echo<<< EOQ
            <th><span title="Experience">exp</span></th>
EOQ;
} else {
    echo<<< EOQ
            <th><span title="Most Valuable Players">mvp</span></th>
EOQ;
}
echo<<< EOQ
            <th>spps</th>
            <th><span title="Skills and other">Skill</span></th>
            <th><span title="Injuries - MNG and Niggles">Injury</span></th>
	</tr>
EOQ;
$matchDao = new MatchDao();
$pmrDao = new PlayerMatchRecordDao();
$pmrs = $pmrDao->findPMRsForPlayer($player);
$pmrs = array_reverse($pmrs);
foreach ($pmrs as $pmr) {
    echoPmr($pmr, $matchDao);
}
echo<<< EOQ
	</table></td></tr>
EOQ;
if ($canEdit) {
    echo<<< EOQ
	<tr><td>
	
		<table cellspacing="0" cellpadding="4">
			<tr>
			<form method="get">
		    <input type="hidden" name="playerId" value="$playerId">
				<td># <select name="roster_num">
EOQ;
    for ($i = 1; $i < 17; $i++) {
        echo "<option value=\"$i\"";
        if ($i == $player->rosterNum) {
            echo " selected";
        }
        echo ">$i</option>";
    }
    echo<<< EOQ
		        </select></td>
	        <td>Name: <input name="name" type="text" size="22" maxlength="64" value="$player->name"/></td>
	        <td><input type="submit" name="submit" value="Update" /></td>
			</tr>
		</table>
	</td></tr>
EOQ;

}
include_once ("includes/footer.php");
?>
